package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.myapplication.IBookManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BookService extends Service {

    private static final String TAG = "AIDL";

    private List<Book> bookList;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "BookService onBind: ");
        bookList = new ArrayList<>();
        return iBinder;
    }


    private IBinder iBinder = new IBookManager.Stub(){


        @Override
        public void addBook(Book book) throws RemoteException {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Log.d(TAG, "BookService addBook: ");
            bookList.add(book);
        }

        @Override
        public List<Book> getBookList() throws RemoteException {
            return bookList;
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "BookService onDestroy: ");
    }
}
